package itdep.common.domain.hibernate;

import itdep.common.domain.Entity;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.Set;

/**
 * @author Ivan Khalopik
 * @version $Revision: 4 $ $Date: 2010-06-10 13:13:16 +0000 (Thu, 10 Jun 2010) $
 */
@MappedSuperclass
public abstract class HibernateEntity<PK extends Serializable> implements Entity<PK> {

	@SuppressWarnings({"UnusedDeclaration"})
	@Version
	@Column(name = "VERSION")
	private Integer version;

	@Column(name = "DELETED", nullable = true, insertable = false, updatable = true)
	private Date deletedAt;

	protected HibernateEntity() {
	}

	public boolean isNew() {
		return getId() == null;
	}

	public Date getDeletedAt() {
		return deletedAt;
	}

	public void setDeletedAt(final Date deletedAt) {
		this.deletedAt = deletedAt;
	}

	public boolean isDeleted() {
		return deletedAt != null;
	}

	public Integer getVersion() {
		return version;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o == null || !(o instanceof HibernateEntity)) {
			return false;
		}
		final HibernateEntity entity = (HibernateEntity) o;
		return !(getId() != null ? !getId().equals(entity.getId()) : entity.getId() != null);
	}

	@Override
	public int hashCode() {
		return getId() == null ? super.hashCode() : getId().hashCode();
	}

	@Override
	public String toString() {
		return "Entity{" +
				"id=" + getId() +
				", class=" + getClass().getSimpleName() +
				"}";
	}

	public Set<Entity<?>> getChildren() {
		return Collections.emptySet();
	}
}
